package com.hnkywl.sts.service.cggl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.cggl.NbcgdDao;
import com.hnkywl.sts.dao.cggl.GlfyDao;
import com.hnkywl.sts.dao.cggl.GlfymxDao;
import com.hnkywl.sts.dao.htgl.CghtDao;
import com.hnkywl.sts.dao.htgl.XshtDao;
import com.hnkywl.sts.dao.xsgl.KhDao;
import com.hnkywl.sts.dao.xsgl.XsdDao;
import com.hnkywl.sts.entity.cggl.Cgd;
import com.hnkywl.sts.entity.cggl.Cgdmx;
import com.hnkywl.sts.entity.cggl.Glfy;
import com.hnkywl.sts.entity.cggl.Glfymx;
import com.hnkywl.sts.entity.htgl.Cght;
import com.hnkywl.sts.entity.htgl.Htmx;
import com.hnkywl.sts.entity.htgl.Xsht;
import com.hnkywl.sts.entity.jhgl.Tpwzjh;
import com.hnkywl.sts.entity.xsgl.Kh;
import com.hnkywl.sts.entity.xsgl.Xsd;
import com.hnkywl.sts.entity.xsgl.Xsmx;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.service.base.SjzdzService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.xsgl.KhService;
import com.hnkywl.sts.service.xsgl.XsdService;
import com.hnkywl.sts.util.CalcUtils;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-7-7
 * Time: 下午3:11
 * To change this template use File | Settings | File Templates.
 */

@Service
public class NbcgdService {
    private NbcgdDao nbcgdDao;
    private KhService khService;
    private KhDao khDao;
    private XsdDao xsdDao;
    private CghtDao cghtDao;
    private XshtDao xshtDao;
    private XsdService xsdService;
    private GlfymxDao glfymxDao;
    private BmService bmService;
    private GlfyDao glfyDao;
    private SjzdzService sjzdzService;

    public String findAllCgdList(String cgdh, Date fhrqStart, Date fhrqEnd, String cglx, Long cggsId, Long cgbmId, Long cgrId, String zt, String sftzd, String htType, int page, int rows) throws ParseException {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page CgdPage = this.nbcgdDao.findAllCgdList(cgdh, fhrqStart, fhrqEnd, cglx, cggsId, cgbmId, cgrId, zt, sftzd, htType, page, rows);
        String CgdJson = gson.toJson(CgdPage.getResult());
        return "{\"total\":" + CgdPage.getTotalCount() + ",\"rows\":" + CgdJson + "}";
    }

    public Page<Cgd> findCgjlQuery(Long cghtId, int pageNo, int pageSize) {
        return this.nbcgdDao.findCgjlQuery(cghtId, pageNo, pageSize);
    }

    public Page<Cgd> findByYtjQuery(String zt, String lx, Long cggsId, Long cgbmId, Long cgrId, String cgdh, String wzbm, int pageNo, int pageSize) {
        return this.nbcgdDao.findByYtjQuery(zt, lx, cggsId, cgbmId, cgrId, cgdh, wzbm, pageNo, pageSize);
    }

    /**
     * 首页采购单数
     *
     * @return
     */
    public String findAllCgd() {
        Integer cgdNum = 0;
        try {
            cgdNum = this.nbcgdDao.findAllCgdList(null, null, null, null, null, null, null, "ybc", "false", "cght", 0, -1).getTotalCount();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cgdNum.toString();
    }

    public Cgd findCgdDetail(Long cgdId) {
        return this.nbcgdDao.get(cgdId);
    }

    @Transactional
    public void editCgdtz(Cgd cgd, Cgd hccgd, Cgd ycgd) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, SQLException {
        cgd.setId(null);
        hccgd.setId(null);

        List<Cgdmx> cgdmxList = ycgd.getCgdmxList();
        List<Glfy> cgfyList = ycgd.getCgfyList();

        List<Cgdmx> hccgdmxList = new ArrayList<Cgdmx>();
        List<Glfy> hcglfyList = new ArrayList<Glfy>();

        for (Cgdmx cgdmx : cgdmxList) {
            Cgdmx hccgdmx = new Cgdmx();
            PropertyUtils.copyProperties(hccgdmx, cgdmx);
            hccgdmx.setId(null);
            hccgdmx.setSbsl(0 - cgdmx.getSbsl());
            hccgdmx.setHsj(0 - cgdmx.getHsj());
            hccgdmx.setBhsj(0 - cgdmx.getBhsj());
            hccgdmxList.add(hccgdmx);
        }
        for (Glfy cgfy : cgfyList) {
            Glfy hccgfy = new Glfy();
            PropertyUtils.copyProperties(hccgfy, cgfy);
            hccgfy.setId(null);
            hccgfy.setJfsl(0 - cgfy.getJfsl());
            hccgfy.setJe(0 - cgfy.getJe());
            hcglfyList.add(hccgfy);
        }

        //保存原采购单
        ycgd.setSftzd(true);
        ycgd.setYcgd(ycgd);
        ycgd.setLastUpdated(new Date());

        //保存红冲的采购单
        hccgd.setCgdh(this.bmService.getCgdbm(hccgd.getCglx()));
        hccgd.setSftzd(true);
        hccgd.setCgdmxList(hccgdmxList);
        hccgd.setCgfyList(hcglfyList);
        hccgd.setCreateUser(YgService.getCurrentYg());
        hccgd.setDateCreated(new Date());
        hccgd.setLastUpdated(hccgd.getDateCreated());
        hccgd.setYcgd(ycgd);

        if (ycgd.getYsgjList() != null && ycgd.getYsgjList().size() == 0) {
            ycgd.setYsgjList(null);
        }
        if (hccgd.getYsgjList() != null && hccgd.getYsgjList().size() == 0) {
            hccgd.setYsgjList(null);
        }
        this.nbcgdDao.save(ycgd);
        this.nbcgdDao.save(hccgd);

        //保存新采购单
        cgd.setCgdh(this.bmService.getCgdbm(cgd.getCglx()));
        cgd.setYcgd(ycgd);
        cgd.setZt(ycgd.getZt());
        cgd.setYrk(ycgd.getYrk());
//        cgd.setYjs(ycgd.getYjs());
        cgd.setCreateUser(YgService.getCurrentYg());
        cgd.setDateCreated(new Date());
        cgd.setLastUpdated(cgd.getDateCreated());
        this.nbcgdDao.save(cgd);
    }


    /**
     * 通过统付统购的采购单生成内部采购和销售单
     *
     * @param cgd
     */
    @Transactional
    public void createTpdj(Cgd cgd) {
        //确定是统配合同

        if (!"tpcght".equals(cgd.getCglx())) {
            return;
        } else if (cgd.getCght().getWzjh() == null
                || ((Tpwzjh) cgd.getCght().getWzjh()).getTcfs() == null
                || !((Tpwzjh) cgd.getCght().getWzjh()).getTcfs().getZ().equals("统付统购")) {
            return;
        }


        //确定申报公司和采购公司不同，主要应对与总部自己统配的情况
        if (cgd.getCggs().equals(cgd.getSbgs())) {
            return;
        }

        //生成申报公司对采购公司的采购单
        this.nbcgdDao.save(createTpCgd(cgd));


        //生成采购公司对申报公司的销售单
        this.xsdDao.save(createTpXsd(cgd));
    }

    private Xsd createTpXsd(Cgd cgd) {
        Xsd xsdnb = new Xsd();
        xsdnb.setXsdh(bmService.getXsdbm("nbxs"));
        xsdnb.setDateCreated(new Date());
        xsdnb.setZt("ysp");
        xsdnb.setCreateUser(cgd.getCreateUser());
        xsdnb.setLastUpdated(xsdnb.getDateCreated());
        xsdnb.setFhsj(xsdnb.getDateCreated());
        xsdnb.setFyzr(sjzdzService.findSjzdzByMc("自提"));
        xsdnb.setYck(true);
//        xsdnb.setYjs(false);
        xsdnb.setBz("统配内部生成销售单");
        xsdnb.setXsgs(cgd.getCggs());
        xsdnb.setSbgs(cgd.getCggs());
        xsdnb.setSbr(cgd.getSbr());
        xsdnb.setXslx("htxs");
        xsdnb.setSxsj(cgd.getCgsj());
        xsdnb.setDsyfje(cgd.getDsyfje());
        xsdnb.setThck(cgd.getDzck());
        xsdnb.setJhsj(cgd.getFhrq());
        xsdnb.setGlId(cgd.getId());
        xsdnb.setKh(cgd.getSbgs());
        xsdnb.setSfnb(true);
        xsdnb.setSl(Double.parseDouble(cgd.getSl()));
        xsdnb.setYsh(true);

        xsdnb.setXsmxList(createXsdmxByCgd(xsdnb, cgd, null));
        return xsdnb;
    }

    private Cgd createTpCgd(Cgd cgd) {
        Cgd cgdnb = new Cgd();
        cgdnb.setCgdh(bmService.getCgdbm("nbcg"));
        cgdnb.setDateCreated(new Date());
        cgdnb.setLastUpdated(cgdnb.getDateCreated());
        cgdnb.setZt("ysp");
        cgdnb.setCreateUser(cgd.getCreateUser());
        cgdnb.setYrk(true);
//        cgdnb.setYjs(false);
        cgdnb.setBz("统配内部生成采购单");
        cgdnb.setCggs(cgd.getSbgs());
        cgdnb.setSbgs(cgd.getSbgs());
        cgdnb.setSbbm(cgd.getSbbm());
        cgdnb.setSbr(cgd.getSbr());
        cgdnb.setCglx("cgrk");
        cgdnb.setCgsj(cgd.getCgsj());
        cgdnb.setCyh(cgd.getCyh());
        cgdnb.setDsyfje(cgd.getDsyfje());
        cgdnb.setDzck(cgd.getDzck());
        cgdnb.setFhgs(cgd.getFhgs());
        cgdnb.setFhrq(cgd.getFhrq());
        cgdnb.setGlId(cgd.getId());
        cgdnb.setGys(cgd.getCggs());
        cgdnb.setJhdhrq(cgd.getJhdhrq());
        cgdnb.setSfnb(true);
        cgdnb.setSl(cgd.getSl());
        cgdnb.setYsh(true);
        cgdnb.setCgdmxList(createCgdmxByCgd(cgdnb, cgd));
        return cgdnb;
    }

    private List<Cgdmx> createCgdmxByCgd(Cgd cgdnb, Cgd cgd) {
        List<Cgdmx> cgdmxes = new ArrayList<Cgdmx>();
        for (Cgdmx cgdmx : cgd.getCgdmxList()) {
            Cgdmx mx = new Cgdmx();
            mx.setId(null);
            mx.setGg(cgdmx.getGg());
            mx.setCd(cgdmx.getCd());
            mx.setJs(cgdmx.getJs());
            mx.setJzfs(cgdmx.getJzfs());
            mx.setSbsl(cgdmx.getSbsl());
            mx.setRkjs(cgdmx.getRkjs());
            mx.setRksl(cgdmx.getRksl());
            mx.setRkjz(cgdmx.getRkjz());
            mx.setBz(cgdmx.getBz());
            mx.setKh(cgdmx.getKh());
            mx.setBhsj(CalcUtils.calcKccb(cgdmx));
            mx.setHsj(CalcUtils.clacHbl(cgdmx.getBhsj(), Double.parseDouble(cgd.getSl())));
            mx.setCgd(cgdnb);
            mx.setLastUpdated(new Date());
            cgdmxes.add(mx);
        }
        return cgdmxes;
    }


    @Transactional
    public String findCgdSearch(String cgdh, Date fhrqStart, Date fhrqEnd, Long cgjsdId, Long cggsId, Long gysId, int pageNo, int pageSize) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page cgdPage = this.nbcgdDao.findCgdSearch(cgdh, fhrqStart, fhrqEnd, cgjsdId, cggsId, gysId, pageNo, pageSize);
        String cgdJson = gson.toJson(cgdPage.getResult());
        return "{\"total\":" + cgdPage.getTotalCount() + ",\"rows\":" + cgdJson + "}";
    }

    @Transactional
    public String findCgdmxSearch(Long cgjsdId) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String result = "";
        Double sbsl = 0d;
        Double hsj = 0d;
        Double bhsj = 0d;
        Double je = 0d;
        Double se = 0d;
        Double ce = 0d;
        List<Map> cgdmxList = this.nbcgdDao.findCgdmxSearch(cgjsdId);
        if (cgdmxList.size() > 0) {
            for (Map map : cgdmxList) {
                sbsl += map.get("sbsl") == null ? 0d : Double.parseDouble(map.get("sbsl").toString());
                hsj += map.get("hsj") == null ? 0d : Double.parseDouble(map.get("hsj").toString());
                bhsj += map.get("bhsj") == null ? 0d : Double.parseDouble(map.get("bhsj").toString());
                je += map.get("je") == null ? 0d : Double.parseDouble(map.get("je").toString());
                se += map.get("se") == null ? 0d : Double.parseDouble(map.get("se").toString());
                ce += map.get("ce") == null ? 0d : Double.parseDouble(map.get("ce").toString());
            }
            result = "{\"rows\":" + gson.toJson(cgdmxList) + ",\"footer\":[{\"cgdh\":\"合计:\",\"sbsl\":" + sbsl
                    + ",\"hsj\":" + hsj + ",\"bhsj\":" + bhsj + ",\"je\":" + je + ",\"se\":" + se + ",\"ce\":" + ce + "}]}";
        } else {
            result = "{\"rows\":" + gson.toJson(cgdmxList) + "}";
        }
        return result;
    }

    @Transactional
    public String findGysByCgd(Long cggsId) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        List<Map> gysList = this.nbcgdDao.findGysByCgd(cggsId);
        String gysJson = gson.toJson(gysList);
        return gysJson;
    }

    @Transactional
    public String findCgdmxByCgdId(Long cgdId, Double ce) {
        Gson gson = new Gson();
        List<Map> cgdmxList = this.nbcgdDao.findCgmx(cgdId);
        Double sbsl = 0d;
        Double average_je = 0d;
        Double zjbc_je = 0d;
        if (cgdmxList.size() > 0) {
            for (Map map : cgdmxList) {
                sbsl += map.get("sbsl") == null ? 0d : (Double) map.get("sbsl");
            }
            average_je = ce / sbsl;
            for (Map map : cgdmxList) {
                zjbc_je = (Double) map.get("sbsl") * average_je;
                map.put("je", zjbc_je);
            }
        }
        return gson.toJson(cgdmxList);
    }

    @Autowired
    public void setNbcgdDao(NbcgdDao nbcgdDao) {
        this.nbcgdDao = nbcgdDao;
    }

    @Autowired
    public void setKhService(KhService khService) {
        this.khService = khService;
    }

    @Autowired
    public void setXsdDao(XsdDao xsdDao) {
        this.xsdDao = xsdDao;
    }

    @Transactional
    public void add(Cgd cgd) {
        initCgd(cgd);
        cgd.setZt("ybc");
        cgd.setCglx("nbcg");
        cgd.setCgdh(bmService.getCgdbm("nbcg"));
        cgd.setCreateOrgan(cgd.getSbgs());
        cgd.setCreateDepartment(cgd.getSbbm());
        if (cgd.getSl() != null) {
            java.text.DecimalFormat df = new java.text.DecimalFormat("#0.0000");
            String sl = df.format(Double.parseDouble(cgd.getSl()));
            cgd.setSl(sl);
        }
        cgd.setCreateUser(YgService.getCurrentYg());
        cgd.setLastUpdated(cgd.getDateCreated());
        if (cgd.getCgfyList() != null && cgd.getCgfyList().size() != 0) {
            for (Glfy glfy : cgd.getCgfyList()) {
                glfy.setDateCreated(new Date());
                glfy.setLastUpdated(new Date());
                glfy.setCreateOrgan(cgd.getSbgs());
                glfy.setCreateDepartment(cgd.getSbbm());
                glfy.setCreateUser(YgService.getCurrentYg());
                glfyDao.save(glfy);
            }
        }
        this.nbcgdDao.save(cgd);
    }

    @Transactional
    public void saveXsdBycgd(Cgd cgd) {
        Xsd tempXsd = cgd.getXsd();
        if (tempXsd.getId() != null) {
            Xsd saveXsd = this.xsdDao.get(tempXsd.getId());
            Xsht xsht = this.xshtDao.get(saveXsd.getXsht().getId());
            saveXsd.setThr(tempXsd.getThr());
            saveXsd.setXsmxList(createXsdmxByCgd(saveXsd, cgd, xsht));
            cgd.setXsd(saveXsd);
            saveXsd.setLastUpdated(new Date());
            this.xsdDao.save(saveXsd);
        } else {
            Xsd saveXsd = tempXsd;
            Xsht xsht = this.xshtDao.get(saveXsd.getXsht().getId());
            saveXsd.setXsgs(cgd.getCggs());
            saveXsd.setXsbm(cgd.getCgbm());
            saveXsd.setXsr(cgd.getCgr());
            saveXsd.setSbgs(cgd.getSbgs());
            saveXsd.setSbbm(cgd.getSbbm());
            saveXsd.setLj(xsht.getLj());
            saveXsd.setSbr(cgd.getSbr());
            saveXsd.setSxsj(cgd.getCgsj());
            saveXsd.setFhsj(saveXsd.getSxsj());
            saveXsd.setFyzr(xsht.getFhzr());
            saveXsd.setSl(xsht.getSl());
            saveXsd.setCreateUser(YgService.getCurrentYg());
            saveXsd.setDateCreated(new Date());
            saveXsd.setLastUpdated(saveXsd.getDateCreated());
            saveXsd.setZt("ybc");
            saveXsd.setXslx("zddh");
            saveXsd.setXsdh(xsdService.xsdBm(saveXsd));
            saveXsd.setXsmxList(createXsdmxByCgd(saveXsd, cgd, xsht));
            saveXsd.setCreateUser(YgService.getCurrentYg());
            saveXsd.setDateCreated(new Date());
            saveXsd.setLastUpdated(saveXsd.getDateCreated());
            this.xsdDao.save(saveXsd);
        }
    }

    //生成销售单明细
    public List<Xsmx> createXsdmxByCgd(Xsd xsd, Cgd cgd, Xsht xsht) {
        List<Xsmx> xsmxes = new ArrayList<Xsmx>();
        for (Cgdmx cgdmx : cgd.getCgdmxList()) {
            Xsmx xsmx = new Xsmx();
            xsmx.setJtwz(cgdmx.getGg());
            xsmx.setJs(cgdmx.getJs());
            xsmx.setXscb(CalcUtils.calcKccb(cgdmx, cgd));
            xsmx.setJzfs(cgdmx.getJzfs());
            Htmx htmx = findHtmx(cgdmx, xsht);
            if (htmx != null) {
                xsmx.setHsj(Double.parseDouble(htmx.getHsj().toString()));
                xsmx.setBhsj(Double.parseDouble(htmx.getBhsj().toString()));
            } else {
                xsmx.setBhsj(CalcUtils.calcKccb(cgdmx, cgd));
                xsmx.setHsj(CalcUtils.clacHbl(xsmx.getBhsj(), Double.parseDouble(cgd.getSl())));
            }
            xsmx.setSbsl(cgdmx.getSbsl());
            xsmx.setBz(cgdmx.getBz());
            xsmx.setDateCreated(new Date());
            xsmx.setLastUpdated(xsmx.getDateCreated());
            xsmx.setCreateUser(YgService.getCurrentYg());
            xsmx.setXsd(xsd);
            xsmxes.add(xsmx);
        }
        return xsmxes;
    }

    private Htmx findHtmx(Cgdmx cgdmx, Xsht xsht) {
        if (xsht != null) {
            for (Htmx htmx : xsht.getHtmxList()) {
                if (htmx.getWzgg().getId().equals(cgdmx.getGg().getId())) {
                    return htmx;
                }
            }
        }

        return null;
    }



    @Transactional
    public void delete(Cgd cgd) {
        cgd = this.nbcgdDao.get(cgd.getId());
        if (cgd.getCgjsd() != null) {
            throw new RuntimeException("禁止删除！该采购单已结算");
        }
//        cgd.setCreateUser(YgService.getCurrentYg());
        cgd.setLastUpdated(new Date());
        cgd.setDeleted(1);
        try {
            if (cgd.getXsd() != null && cgd.getXsd().getId() != null) {
                Xsd xsd = cgd.getXsd();
                xsd = this.xsdDao.get(xsd.getId());
//                xsd.setCreateUser(YgService.getCurrentYg());
                xsd.setDeleted(1);
                xsd.setLastUpdated(cgd.getLastUpdated());
            }
        } catch (Exception e) {

        }
        this.nbcgdDao.save(cgd);
    }

    public Cgd findById(Long id) {
        return this.nbcgdDao.get(id);
    }

    public List<Map> findCgmx(Long id) {
        return this.nbcgdDao.findCgmx(id);
    }

    /**
     * 采购结算查询采购单明细
     * @param id
     * @return
     */
    public List<Map> findCgjsdSearchCgmx(Long id) {
        return this.nbcgdDao.findCgjsdSearchCgmx(id);
    }


    public List<Map> findCgmxByKh(Kh kh) {
        return this.nbcgdDao.findCgmxByKh(kh);
    }

    public List<Map> findCgfy(Long id) {
        return this.nbcgdDao.findCgfy(id);
    }


    @Transactional
    public void stock(Cgd cgd) throws SQLException {
        List<Cgdmx> cgdmxes = cgd.getCgdmxList();
        Cgd saveCgd = this.nbcgdDao.get(cgd.getId());
        setCgdIntoCgdmx(saveCgd, cgdmxes);
        saveCgd.setCgdmxList(cgdmxes);
        saveCgd.setYrk(true);
        saveCgd.setZt("ysp");
        saveCgd.setRkr(YgService.getCurrentYg());
        saveCgd.setLastUpdated(new Date());
        saveCgd.setRksj(new Date());
        initCgd(saveCgd);
        saveKhs(cgdmxes, saveCgd);
        this.nbcgdDao.save(saveCgd);


        createTpdj(saveCgd);

        if (saveCgd.getCgfyList() != null) {
            List<Glfy> glfyList = saveCgd.getCgfyList();
            Double sumSl = sumSl(cgdmxes);
            for (Glfy glfy : glfyList) {
                for (Cgdmx cgdmx : cgdmxes) {
                    Glfymx glfymx = new Glfymx();
                    glfymx.setGg(cgdmx.getGg());
                    glfymx.setGlfy(glfy);
                    glfymx.setJtfy(glfy.getJe() / sumSl * cgdmx.getRksl());
                    glfymx.setLastUpdated(new Date());
                    glfymxDao.save(glfymx);
                }
                glfy.setLastUpdated(new Date());
                glfyDao.save(glfy);
            }
        }
    }

    private Double sumSl(List<Cgdmx> cgdmxList) {
        Double sumSl = 0d;
        for (Cgdmx cgdmx : cgdmxList) {
            sumSl += cgdmx.getRksl();
        }
        return sumSl;
    }

    private void setCgdIntoCgdmx(Cgd cgd, List<Cgdmx> cgdmxes) {
        for (Cgdmx cgdmx : cgdmxes) {
            cgdmx.setCgd(cgd);
        }
    }

    @Transactional
    private void saveKhs(List<Cgdmx> cgdmxes, Cgd cgd) throws SQLException {
        for (Cgdmx cgdmx : cgdmxes) {
            Kh kh = new Kh();
            kh.setKhbm(bmService.getKckhbm(cgd.getDzck().getId()));
            kh.setJtwz(cgdmx.getGg());
            kh.setCggs(cgd.getCggs());
            kh.setSbgs(cgd.getSbgs());
            kh.setSbbm(cgd.getSbbm());
            kh.setCd(cgdmx.getCd());
            kh.setCgd(cgd);
            kh.setSl(cgdmx.getRksl());
            kh.setJs(cgdmx.getRkjs());
            kh.setJz(cgdmx.getRkjz());
            kh.setKkdsl(cgdmx.getRksl());
            kh.setWsdj(CalcUtils.calcKccb(cgdmx));
            kh.setCk(cgd.getDzck());
            kh.setZlqk("");
            kh.setRkrq(cgd.getRksj());
            kh.setBz("");
            kh.setCreateUser(YgService.getCurrentYg());
            kh.setDateCreated(new Date());
            kh.setLastUpdated(kh.getDateCreated());
            cgdmx.setKh(kh);
            this.khService.save(kh);
        }
    }

    private void initCgd(Cgd cgd) {
        if (cgd.getCght() != null && cgd.getCght().getId() == null) {
            cgd.setCght(null);
        }
        if (cgd.getGys() != null && cgd.getGys().getId() == null) {
            cgd.setGys(null);
        }
        if (cgd.getDzck() != null && cgd.getDzck().getId() == null) {
            cgd.setDzck(null);
        }
        if (cgd.getFhgs() != null && cgd.getFhgs().getId() == null) {
            cgd.setFhgs(null);
        }
        if (cgd.getYsdw() != null && cgd.getYsdw().getId() == null) {
            cgd.setYsdw(null);
        }

        if (cgd.getCggs() != null && cgd.getCggs().getId() == null) {
            cgd.setCggs(null);
        }
        if (cgd.getCgbm() != null && cgd.getCgbm().getId() == null) {
            cgd.setCgbm(null);
        }
        if (cgd.getCgr() != null && cgd.getCgr().getId() == null) {
            cgd.setCgr(null);
        }
        if (cgd.getSbgs() != null && cgd.getSbgs().getId() == null) {
            cgd.setSbgs(null);
        }
        if (cgd.getSbbm() != null && cgd.getSbbm().getId() == null) {
            cgd.setSbbm(null);
        }
        if (cgd.getSbr() != null && cgd.getSbr().getId() == null) {
            cgd.setSbr(null);
        }

        if (cgd.getCgdmxList() != null) {
            for (Cgdmx cgdmx : cgd.getCgdmxList()) {
                if (cgdmx.getJzfs() != null && cgdmx.getJzfs().getId() == null) {
                    cgdmx.setJzfs(null);
                }
                if (cgdmx.getCd() != null && cgdmx.getCd().getId() == null) {
                    cgdmx.setCd(null);
                }
            }
        }
    }

    @Transactional
    public void confirm(Cgd cgd) {
        cgd = this.nbcgdDao.get(cgd.getId());
        cgd.setZt("ytj");
        if ("zddh".equals(cgd.getCglx())) {
            if (cgd.getXsd() != null) {
                cgd.getXsd().setZt("ytj");
            }
        }
        cgd.setLastUpdated(new Date());
        this.nbcgdDao.save(cgd);
    }

    @Transactional
    public void cancel(Cgd cgd) {
        cgd = this.nbcgdDao.get(cgd.getId());
        cgd.setZt("ybc");
        if ("zddh".equals(cgd.getCglx())) {
            if (cgd.getXsd() != null) {
                cgd.getXsd().setZt("ybc");
            }
        }
        cgd.setLastUpdated(new Date());
        this.nbcgdDao.save(cgd);
    }

    @Autowired
    public void setKhDao(KhDao khDao) {
        this.khDao = khDao;
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }

    @Autowired
    public void setCghtDao(CghtDao cghtDao) {
        this.cghtDao = cghtDao;
    }

    @Autowired
    public void setXsdService(XsdService xsdService) {
        this.xsdService = xsdService;
    }

    @Autowired
    public void setXshtDao(XshtDao xshtDao) {
        this.xshtDao = xshtDao;
    }

    @Autowired
    public void setGlfymxDao(GlfymxDao glfymxDao) {
        this.glfymxDao = glfymxDao;
    }

    @Autowired
    public void setGlfyDao(GlfyDao glfyDao) {
        this.glfyDao = glfyDao;
    }

    @Autowired
    public void setSjzdzService(SjzdzService sjzdzService) {
        this.sjzdzService = sjzdzService;
    }
}
